﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BDSim
{
    //  [add by [Penghaijun] 2015.1.16
    /// <summary>
    /// ColorRank.xaml 的交互逻辑
    /// </summary>
    public partial class ColorRankView : UserControl
    {
        public ColorRankView()
        {
            InitializeComponent();

            Init(DopEnum.Coverage);
        }

        #region 属性和成员
        /// <summary>
        /// 颜色字典
        /// </summary>
        Dictionary<int, Brush> DicColor = new Dictionary<int, Brush>()
        {
            {0,new SolidColorBrush(Color.FromRgb(230,60,0))},
            {1,new SolidColorBrush(Color.FromRgb(255,141,0))},
            {2,new SolidColorBrush(Color.FromRgb(255,255,0))},
            {3,new SolidColorBrush(Color.FromRgb(192,255,0))},
            {4,new SolidColorBrush(Color.FromRgb(61,249,61))},
            {5,new SolidColorBrush(Color.FromRgb(33,204,20))},
        };

        /// <summary>
        /// 倒序颜色字典 
        /// </summary>
        List<Brush> ReverseDicColor = new List<Brush>()
        {
            {new SolidColorBrush(Color.FromRgb(33,204,20))},
            {new SolidColorBrush(Color.FromRgb(61,249,61))},
            {new SolidColorBrush(Color.FromRgb(192,255,0))},
            {new SolidColorBrush(Color.FromRgb(255,255,0))},
            {new SolidColorBrush(Color.FromRgb(255,141,0))},
            {new SolidColorBrush(Color.FromRgb(230,60,0))},
        };

        /// <summary>
        /// 颜色的种类
        /// </summary>
        int colorNum = 0;

        /// <summary>
        /// 当前性能分析的类型
        /// </summary>
        DopEnum curDopType = DopEnum.Coverage;

        /// <summary>
        /// 颜色种类 
        /// </summary>
        public int ColorNum
        {
            get { return colorNum; }
            set { colorNum = value; }
        }

        /// <summary>
        /// 颜色梯度指示值，Label控件显示
        /// </summary>
        List<Label> labelList = new List<Label>();//显示图例对应的数值

        #endregion

        #region 初始化颜色等级
        /// <summary>
        /// 初始化颜色等级 
        /// </summary>
        /// <param name="dopType"></param>
        void Init(DopEnum dopType)
        {
            List<Brush> tempColorDic;
            //为了使较好的性能都显示为绿色，出了覆盖性，其他性能都是用倒序颜色字典
            if (dopType == DopEnum.Coverage)
            {
                tempColorDic = DicColor.Values.ToList();
            }
            else
            {
                tempColorDic = ReverseDicColor;
            }
            colorNum = tempColorDic.Count;//颜色种类
            int ColNum = 1;//网格列数
            int RowNum = tempColorDic.Count;//网格行数 
            //清除网格
            colorGrid.ColumnDefinitions.Clear();
            colorGrid.RowDefinitions.Clear();
            //绘制网格的行 
            for (int j = 0; j <= RowNum; j++)
            {
                RowDefinition row = new RowDefinition();
                colorGrid.RowDefinitions.Add(row);
            }
            colorGrid.RowDefinitions.Last().Height = new GridLength(0);
            //colorGrid.RowDefinitions[0].Height = new GridLength(25, GridUnitType.Pixel);
            //绘制网格的列 
            for (int i = 0; i < ColNum; i++)
            {
                ColumnDefinition col = new ColumnDefinition();
                colorGrid.ColumnDefinitions.Add(col);
            }
            //在每个网格中添加Grid              
            for (int i = 0; i <= RowNum; i++)
            {
                Grid grid = new Grid();
                Grid.SetRow(grid, i);
                Grid.SetColumn(grid, 0);
                colorGrid.Children.Add(grid);
                if (i != RowNum)
                {
                    grid.Background = tempColorDic[i];
                }
                else
                {
                    grid.Background = Brushes.Transparent;
                }

                Label label = new Label();
                if (i < RowNum + 1)
                {
                    labelList.Add(label);
                }
            }
        }
        #endregion

        #region 设置图例对应的数值
        /// <summary>
        /// 设置图例对应的数值
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        public void SetFigure(double min, double max, DopEnum dopType)
        {
            if (curDopType != dopType)
            {
                Init(dopType);
                curDopType = dopType;
            }
            double gap = (max - min) / colorNum;
            double labelContent=0.0;

            for (int i = 0; i <= colorNum; i++)
            {
                if (colorGrid.Children.Contains(labelList[i]))
                {
                    colorGrid.Children.Remove(labelList[i]);
                }

                //把指标梯度值Label控件加到Grid中
                Grid.SetRow(labelList[i], i);
                Grid.SetColumn(labelList[i], 0);
                colorGrid.Children.Add(labelList[i]);
                labelList[i].FontSize = 12;
                labelList[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                //“i”表示第几个网格
                if (i == 0)
                {
                    ///设置Label的位置
                    labelList[i].Margin = new Thickness(-5, -5, 0, 0);
                }
                else if (i>0 && i<6)
                {
                    ///设置Label的位置
                    labelList[i].Margin = new Thickness(-5, -5, 0, 0);
                }
                else if (i == 6)
                {
                    labelList[i].VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                    ///设置Label的位置
                    labelList[i].Margin = new Thickness(-5, -30, 0, 0);
                }
                if (i != colorNum)
                {
                    labelContent = min + gap * i;
                }
                else
                {
                    labelContent = max;
                }
                ///设置Label的内容，并制定显示格式
                labelList[i].Content = labelContent.ToString("F01");
            }
        }
        #endregion
    }
}
